########################################################################################

library(data.table)
library(RColorBrewer)
library(ggplot2)
library(argparser)
library(dplyr)

##############################################################################

argp <- arg_parser("Plot the deconstructSigs")
argp <- add_argument(argp, "--sig_file" , help="")
argp <- add_argument(argp, "--images_path" , help="")
argp <- add_argument(argp, "--info_file" , help="")
argp <- add_argument(argp, "--info_public_file" , help="")

argv <- parse_args(argp)

sig_file <- argv$sig_file
images_path <- argv$images_path
info_file <- argv$info_file
info_public_file <- argv$info_public_file

if(1!=1){

  images_path <- "~/20220915_gastric_multiple/dna_combinePublic/sigProfiler/plot_allUSE"
  info_file <- paste("~/20220915_gastric_multiple/dna_combinePublic/baseTable/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv",sep="")
	info_public_file <- paste("~/20220915_gastric_multiple/dna_combinePublic/public_ref/combine/MutationInfo.combine.addMolecularSubType.tsv",sep="")
	sig_file <- "~/20220915_gastric_multiple/dna_combinePublic/sigProfiler/decompose_allUSE/combine_SBS96.txt"

}

##############################################################################

dir.create(images_path , recursive = T)

dat_info_nmu <- data.frame(fread(info_file))
dat_info_public <- data.frame(fread(info_public_file))
dat_allSBS <- data.frame(fread(sig_file))

#########################################################################
## 得到比例
dat_allSBS <- melt(dat_allSBS)
dat_allSBS_ratio <- dat_allSBS %>%
group_by( Samples ) %>%
summarize( variable = variable , value = value , ratio = value/sum(value) )

#########################################################################

dat_allSBS_ratio$Samples[grep( "TCGA" , dat_allSBS_ratio$Samples )] <- substring(grep( "TCGA" , dat_allSBS_ratio$Samples , value = T ) , 1 , 12)

#########################################################################
## 标记来源
dat_info_nmu_use <- dat_info_nmu[,c("Tumor" , "Patient" , "Class" , "Type" , "TCGA_Class")]
dat_info_nmu_use$From <- "NJMU"
dat_info_public$Type <- dat_info_public$Class
dat_info_public <- dat_info_public[,c("Tumor" , "Tumor" , "Class" , "Type" , "Molecular.subtype" , "From")]
dat_info_public <- subset( dat_info_public , From != "NJMU" )
col_use <- c("Tumor" , "Patient" , "Class" , "Type" , "Molecular.subtype" , "From")
colnames(dat_info_nmu_use) <- col_use
colnames(dat_info_public) <- col_use
dat_info_public$mut_class <- "all"
## all
dat_info_nmu_use1 <- dat_info_nmu_use
dat_info_nmu_use1$Tumor <- paste0( dat_info_nmu_use1$Tumor , "_all" )
dat_info_nmu_use1$mut_class <- "all"
## private
dat_info_nmu_use2 <- dat_info_nmu_use
dat_info_nmu_use2$Tumor <- paste0( dat_info_nmu_use2$Tumor , "_private" )
dat_info_nmu_use2$mut_class <- "private"
## trunk
dat_info_nmu_use3 <- dat_info_nmu_use
dat_info_nmu_use3$Tumor <- paste0( dat_info_nmu_use3$Tumor , "_trunk" )
dat_info_nmu_use3$mut_class <- "trunk"

## 所有用到的样本信息
info_all_use <- rbind( dat_info_public , dat_info_nmu_use1 , dat_info_nmu_use2 , dat_info_nmu_use3 )

## 合并信号
dat_allSBS_ratio2 <- merge( dat_allSBS_ratio , info_all_use , by.x = "Samples" , by.y = "Tumor" )

out_name <- paste0( images_path , "/combine_SBS96.ratio.addInfo.tsv" )
write.table( dat_allSBS_ratio2 , out_name , row.names = F , sep = "\t" , quote = F  )